#!/bin/sh
#
# Copyright 2005 Justin Erenkrantz and Greg Stein
# Copyright 2005 The Apache Software Foundation or its licensors, as
# applicable.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# buildconf: Build the support scripts needed to compile from a
#            checked-out version of the source code.

# set a couple of defaults for where we should be looking for our support libs.
# can be overridden with --with-apr=[dir] and --with-apr-util=[dir]

apr_src_dir="apr ../apr"
apu_src_dir="apr-util ../apr-util"

while test $# -gt 0 
do
  # Normalize
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case "$1" in
  --with-apr=*)
  apr_src_dir=$optarg
  ;;
  esac

  case "$1" in
  --with-apr-util=*)
  apu_src_dir=$optarg
  ;;
  esac

  shift
done

#
# Check to be sure that we have the srclib dependencies checked-out
#

should_exit=0
apr_found=0
apu_found=0

for dir in $apr_src_dir
do
    if [ -d "${dir}" -a -f "${dir}/build/apr_common.m4" ]; then
        echo "found apr source: ${dir}"
        apr_src_dir=$dir
        apr_found=1
        break
    fi
done

if [ $apr_found -lt 1 ]; then
    echo ""
    echo "You don't have a copy of the apr source in srclib/apr. "
    echo "Please get the source using the following instructions," 
    echo "or specify the location of the source with " 
    echo "--with-apr=[path to apr] :"
    echo ""
    echo "   svn co http://svn.apache.org/repos/asf/apr/apr/trunk srclib/apr"
    echo ""
    should_exit=1
fi

for dir in $apu_src_dir
do
    if [ -d "${dir}" -a -f "${dir}/Makefile.in" ]; then
        echo "found apr-util source: ${dir}"
        apu_src_dir=$dir
        apu_found=1
        break
    fi
done

if [ $apu_found -lt 1 ]; then
    echo ""
    echo "APR-util not found.  Assuming you are using APR 2.x."
    echo ""
    apu_src_dir=$apr_src_dir
fi

if [ $should_exit -gt 0 ]; then
    exit 1
fi

if [ ! -d build ]; then
  $apr_src_dir/build/mkdir.sh build
fi

echo copying build files
cp $apr_src_dir/build/config.guess $apr_src_dir/build/config.sub \
   $apr_src_dir/build/install.sh $apr_src_dir/build/apr_common.m4 \
   $apr_src_dir/build/find_apr.m4 $apr_src_dir/build/get-version.sh build

if [ -n $apu_src_dir -a -d $apu_src_dir ] ; then
 cp $apu_src_dir/build/find_apu.m4 build
fi

echo generating configure
${AUTOCONF:-autoconf}

# Remove autoconf 2.5x's cache directory
rm -rf autom4te*.cache
